/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class UpdateUser extends AbstractAction
/*     */ {
/*  31 */   private static Logger logger = Logger.getLogger(UpdateUser.class.getName());
/*     */ 
/*  33 */   private String id = null;
/*     */ 
/*     */   public UpdateUser(AbstractHttpServlet servlet) {
/*  36 */     super(servlet);
/*     */   }
/*     */ 
/*     */   public UpdateUser(AbstractHttpServlet servlet, String id) {
/*  40 */     super(servlet);
/*  41 */     this.id = id;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  47 */     response.setContentType("text/html;charset=utf-8");
/*  48 */     PrintWriter out = response.getWriter();
/*  49 */     JSONObject jsonObject = new JSONObject();
/*     */ 
/*  51 */     User loginUser = super.getLoginUser(request);
/*     */ 
/*  55 */     if ((!loginUser.isAdmin) && (!loginUser.isGroupAdmin)) {
/*  56 */       showErrorMessage(response, "没有权限修改");
/*  57 */       return;
/*     */     }
/*     */ 
/*  61 */     if (!loginUser.isAdmin)
/*     */     {
/*  63 */       CompanyInfo company = super.getCompany(request);
/*     */ 
/*  65 */       User user = company.getUser(this.id);
/*  66 */       if (user == null) {
/*  67 */         showErrorMessage(response, "您要修改的用户不存在");
/*  68 */         return;
/*     */       }
/*     */ 
/*  71 */       int userGroupId = user.groupId;
/*     */ 
/*  73 */       if (userGroupId == 0) {
/*  74 */         showErrorMessage(response, "没有权限修改");
/*  75 */         return;
/*     */       }
/*     */ 
/*  79 */       if (!company.isGroupAdmin(loginUser, userGroupId)) {
/*  80 */         showErrorMessage(response, "没有权限修改");
/*  81 */         return;
/*     */       }
/*     */     }
/*     */ 
/*  85 */     String updateType = Util.getURLParameter(request
/*  86 */       .getParameter("updateType"));
/*     */ 
/*  89 */     if ("group".equals(updateType))
/*     */     {
/*  91 */       if (!loginUser.isAdmin) {
/*  92 */         showErrorMessage(response, "没有权限修改");
/*  93 */         return;
/*     */       }
/*     */ 
/*  96 */       int groupId = Util.getURLIntParameter(request
/*  97 */         .getParameter("groupId"));
/*  98 */       int isGroupAdmin = Util.getURLIntParameter(request
/*  99 */         .getParameter("isGroupAdmin"));
/* 100 */       if (groupId == 0) {
/* 101 */         isGroupAdmin = 0;
/*     */       }
/*     */ 
/* 104 */       UserManager manager = new UserManager();
/*     */       try {
/* 106 */         int result = manager
/* 107 */           .updateGroup(this.id, groupId, isGroupAdmin);
/* 108 */         if (result == 1)
/*     */         {
/* 110 */           CompanyInfo company = super.getCompany(loginUser.companyId);
/* 111 */           company.users = manager.getCompanyUsers(
/* 112 */             loginUser.companyId, null);
/*     */         }
/* 114 */         jsonObject.put("error", "0");
/*     */       } catch (Exception e) {
/* 116 */         jsonObject.put("error", "1");
/* 117 */         logger.warn("modify user group failed", e);
/*     */       }
/* 119 */       out.println(jsonObject);
/* 120 */       out.flush();
/* 121 */       out.close();
/* 122 */       return;
/*     */     }
/*     */ 
/* 126 */     if ("password".equals(updateType)) {
/* 127 */       String password = Util.getURLParameter(request
/* 128 */         .getParameter("password"));
/* 129 */       String comfirmPassword = Util.getURLParameter(request
/* 130 */         .getParameter("confirmpassword"));
/* 131 */       if ((password != null) && (!password.equals(comfirmPassword))) {
/* 132 */         jsonObject.put("error", "1");
/*     */       } else {
/* 134 */         UserManager manager = new UserManager();
/*     */         try {
/* 136 */           manager.updatePassword(this.id, password);
/* 137 */           jsonObject.put("error", "0");
/*     */         } catch (Exception e) {
/* 139 */           jsonObject.put("error", "1");
/* 140 */           logger.warn("modify user password failed", e);
/*     */         }
/*     */       }
/* 143 */       out.println(jsonObject);
/* 144 */       out.flush();
/* 145 */       out.close();
/* 146 */       return;
/*     */     }
/*     */ 
/* 150 */     UserManager manager = new UserManager();
/* 151 */     String operationFlag = Util.getURLParameter(request
/* 152 */       .getParameter("operationFlag"));
/* 153 */     int isAdmin = Util.getURLIntParameter(request.getParameter("isAdmin"));
/*     */     try
/*     */     {
/* 156 */       if ((operationFlag != null) && (operationFlag.trim().length() > 0))
/*     */       {
/* 158 */         manager.updateOperationFlag(this.id, operationFlag);
/*     */ 
/* 161 */         User u = getUser(request, this.id);
/* 162 */         if (u != null) {
/* 163 */           u.canExport = "1".equals(Util.getArrayValue(
/* 164 */             operationFlag.split(","), 
/* 165 */             0));
/*     */ 
/* 167 */           u.canDelete = "1".equals(Util.getArrayValue(
/* 168 */             operationFlag.split(","), 
/* 169 */             1));
/*     */ 
/* 172 */           if (this.id.equals(loginUser.userId)) {
/* 173 */             loginUser.canExport = u.canExport;
/* 174 */             loginUser.canDelete = u.canDelete;
/*     */           }
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/* 180 */         if (!loginUser.isAdmin) {
/* 181 */           showErrorMessage(response, "没有权限修改");
/* 182 */           return;
/*     */         }
/*     */ 
/* 185 */         manager.updateIsAdmin(this.id, isAdmin);
/* 186 */         User u = getUser(request, this.id);
/* 187 */         if (u != null) {
/* 188 */           u.isAdmin = (isAdmin == 1);
/*     */ 
/* 190 */           if (this.id.equals(loginUser.userId)) {
/* 191 */             loginUser.isAdmin = u.isAdmin;
/*     */           }
/*     */         }
/*     */       }
/* 195 */       jsonObject.put("error", "0");
/*     */     } catch (Exception e) {
/* 197 */       logger.warn("updage user info failed", e);
/* 198 */       jsonObject.put("error", "1");
/*     */     }
/* 200 */     out.println(jsonObject);
/* 201 */     out.flush();
/* 202 */     out.close();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.UpdateUser
 * JD-Core Version:    0.6.1
 */